<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%String path = request.getContextPath();%>
<%@ taglib uri="/WEB-INF/tld/c.tld" prefix="c" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>人工修改回购信息</title>
    <jsp:include page="../../import.jsp" flush="true"/>
    <style type="text/css">
        .searchArea .fieldBox .label{width: 60px; overflow: hidden; white-space: nowrap; text-overflow: ellipsis;}
    </style>
</head>

<body class="iframeBody">
<div class="Wrapper">

    <div id="ligergrid1" class="sq-grid"></div>
    <input name="billId" id="billId" type="hidden" value="${billId}">
    <span id="saveBtn" class="sq-button sq-button-round">确认修改</span>
    <span id="quxiao" class="sq-button sq-button-round">取消</span>
</div>

<script type="text/javascript">
    var grid = null, callback = null;
    var billId = $("#billId").val();
    $(function () {
        grid = $("#ligergrid1").ligerGrid({
            url: '<%=path%>/mcr/mcrBillBuyBackRecord/mcrBuyBackFailDetail.do?billId='+billId,
            method: 'get',
            columns: [
            	{display: '订单号', name: 'billId', minWidth: 60, align: 'center', enabledSort: false},
            	{display: '回购批次号', name: 'sqBuyBackBatchNo', minWidth: 60, align: 'center', enabledSort: false},
            	{display: '合同号', name: 'crCntNo', minWidth: 120, align: 'center', enabledSort: false},
            	{display: '客户姓名', name: 'cstNm', minWidth: 60, align: 'center', enabledSort: false},
            	{display: '放款时间',  name: 'crDt', minWidth: 100, align: 'center', enabledSort: false,render:renderDate },
            	{display: '狮桥回购总金额', name: 'totalAmt', minWidth: 100, align: 'center', enabledSort: false,
	                render: function (item) {
	                    var h = '&yen; ' + SQ.utils.currencyFormatter(item.totalAmt, 2)
	                    return h;
	                },
	            },
            	{display: '狮桥未还本金', name: 'capAmt', minWidth: 100, align: 'center', enabledSort: false,
	            		editor: {type: 'number',
	                    precision: 2,
	                    toFix: true,
	                    onToFix: function () {
	                        $.ligerDialog.SQalert({
	                            SQ_type: "warning",
	                            content: "小数点后面最多2位！"
	                        });
	                    }
	                },
	                render: function (item) {
	                    var h = '&yen; ' + SQ.utils.currencyFormatter(item.capAmt, 2)
	                    return h;
	                },
                },
            	{display: '狮桥未还利息', name: 'intrAmt', minWidth: 100, align: 'center', enabledSort: false,
	            		editor: {type: 'number',
	                    precision: 2,
	                    toFix: true,
	                    onToFix: function () {
	                        $.ligerDialog.SQalert({
	                            SQ_type: "warning",
	                            content: "小数点后面最多2位！"
	                        });
	                    }
	                },
	                render: function (item) {
	                    var h = '&yen; ' + SQ.utils.currencyFormatter(item.intrAmt, 2)
	                    return h;
	                },
                },
            	{display: '狮桥未还罚息', name: 'penaltyAmt', minWidth: 100, align: 'center', enabledSort: false,
	            		editor: {type: 'number',
	                    precision: 2,
	                    toFix: true,
	                    onToFix: function () {
	                        $.ligerDialog.SQalert({
	                            SQ_type: "warning",
	                            content: "小数点后面最多2位！"
	                        });
	                    }
	                },
	                render: function (item) {
	                    var h = '&yen; ' + SQ.utils.currencyFormatter(item.penaltyAmt, 2)
	                    return h;
	                },
                },
            	{display: '银行应回购总金额', name: 'yhTotalAmt', minWidth: 100, align: 'center', enabledSort: false,
	                render: function (item) {
	                    var h = '&yen; ' + SQ.utils.currencyFormatter(item.yhTotalAmt, 2)
	                    return h;
	                },
	            },
            	{display: '银行应回购本金', name: 'yhCapAmt', minWidth: 100, align: 'center', enabledSort: false,
	            		editor: {type: 'number',
	                    precision: 2,
	                    toFix: true,
	                    onToFix: function () {
	                        $.ligerDialog.SQalert({
	                            SQ_type: "warning",
	                            content: "小数点后面最多2位！"
	                        });
	                    }
	                },
	                render: function (item) {
	                    var h = '&yen; ' + SQ.utils.currencyFormatter(item.yhCapAmt, 2)
	                    return h;
	                },
                },
            	{display: '银行应回购利息', name: 'yhIntrAmt', minWidth: 100, align: 'center', enabledSort: false,
	            		editor: {type: 'number',
	                    precision: 2,
	                    toFix: true,
	                    onToFix: function () {
	                        $.ligerDialog.SQalert({
	                            SQ_type: "warning",
	                            content: "小数点后面最多2位！"
	                        });
	                    }
	                },
	                render: function (item) {
	                    var h = '&yen; ' + SQ.utils.currencyFormatter(item.yhIntrAmt, 2)
	                    return h;
	                },
                },
            	{display: '银行应回购罚息', name: 'yhPenaltyAmt', minWidth: 100, align: 'center', enabledSort: false,
	            		editor: {type: 'number',
	                    precision: 2,
	                    toFix: true,
	                    onToFix: function () {
	                        $.ligerDialog.SQalert({
	                            SQ_type: "warning",
	                            content: "小数点后面最多2位！"
	                        });
	                    }
	                },
	                render: function (item) {
	                    var h = '&yen; ' + SQ.utils.currencyFormatter(item.yhPenaltyAmt, 2)
	                    return h;
	                },
                },
            ],
            onAfterEdit: function (e) {
            	var totalAmt = e.record.totalAmt;//狮桥回购总金额
            	var capAmt = e.record.capAmt;//狮桥未还本金
            	var intrAmt = e.record.intrAmt;//狮桥未还利息
            	var penaltyAmt = e.record.penaltyAmt;//狮桥未还罚息
            	var yhTotalAmt = e.record.yhTotalAmt;//银行应回购总金额
            	var yhCapAmt = e.record.yhCapAmt;//银行应回购本金
            	var yhIntrAmt = e.record.yhIntrAmt;//银行应回购利息
            	var yhPenaltyAmt = e.record.yhPenaltyAmt;//银行应回购罚息
            	
               	if(e.column.name =="capAmt" || e.column.name =="intrAmt" || e.column.name =="penaltyAmt"){
               		shTotalAmt = Number(capAmt) + Number(intrAmt) + Number(penaltyAmt);
               		grid.updateCell('totalAmt', shTotalAmt, e.record);	 
             	}
            	
               	if(e.column.name =="yhCapAmt" || e.column.name =="yhIntrAmt" || e.column.name =="yhPenaltyAmt"){
               		yhTotalAmt = Number(yhCapAmt) + Number(yhIntrAmt) + Number(yhPenaltyAmt);
               		grid.updateCell('yhTotalAmt', yhTotalAmt, e.record);	 
             	} 
              }, 
            
            pageSize: 10,
            root: 'rows',
            record: 'total',
            hideLoadButton: true,
            width: '100%',
            height: '100%',
            heightDiff: -2,
            checkbox: false,
            rownumbers: true,
            allowUnSelectRow: true,
            alternatingRow: false,
            enabledEdit : true
        });
        grid.setHeight(100);
        grid.toggleCol('billId', false);
        grid.toggleCol('sqBuyBackBatchNo', false);
        $("#searchBar").ligerForm();
        /* 查询 */
        $("#searchBtn").click(function (event) {
            var form = liger.get("searchBar");
            var data = form.getData();
            for (name in data) {
                grid.setParm(name, data[name]);
            }
            grid.reload();
        });

        /* 清空 */
        $("#clearBtn").click(function (event) {
            clearLigerForm("searchBar");
        });

        callback = function () {
            grid.reload();
        };
        
        //取消
        $("#quxiao").click(function(event) {
        	frameElement.dialog.close();
        });
        
        $("#saveBtn").click(function(event) {
        	top.$.ligerDialog.confirm("是否再次发起回购?", function (flag) {
        		var billId = grid.rows[0].billId;
        		var sqBuyBackBatchNo = grid.rows[0].sqBuyBackBatchNo;
        		var totalAmt = grid.rows[0].totalAmt;
            	var capAmt = grid.rows[0].capAmt;
            	var intrAmt = grid.rows[0].intrAmt;
            	var penaltyAmt = grid.rows[0].penaltyAmt;
            	var yhTotalAmt = grid.rows[0].yhTotalAmt;
            	var yhCapAmt = grid.rows[0].yhCapAmt;
            	var yhIntrAmt = grid.rows[0].yhIntrAmt;
            	var yhPenaltyAmt = grid.rows[0].yhPenaltyAmt;
            	var data = {};
            	data["billId"] = billId;
            	data["sqBuyBackBatchNo"] = sqBuyBackBatchNo;
            	data["totalAmt"] = totalAmt;
            	data["capAmt"] = capAmt;
            	data["intrAmt"] = intrAmt;
            	data["penaltyAmt"] = penaltyAmt;
            	data["yhTotalAmt"] = yhTotalAmt;
            	data["yhCapAmt"] = yhCapAmt;
            	data["yhIntrAmt"] = yhIntrAmt;
            	data["yhPenaltyAmt"] = yhPenaltyAmt;
            	console.log("data>>"+JSON.stringify(data));
            	var url = '<%=path%>/mcr/mcrBillBuyBackRecord/mcrBuyBackFailcommit.do';
                $.ajax({
                    type: 'post',
                    dataType: 'json',
                    data:data,
                    url: url,
                    beforeSend: function(){
                        wait_dialog=$.ligerDialog.SQalert({content: LogUtils.LOG_CMM_DATA_LOADING ,SQ_type: "waitting" });
                    },
                    success:function(msg){
                        wait_dialog.close();
                        if(msg.success == 1){
                        	 top.$.ligerDialog.SQalert({content: msg.info,SQ_type: "success"});
                        	 parent.location.reload();
                             frameElement.dialog.close();
                        }else{
                            $.ligerDialog.SQalert({content: msg.info,SQ_type: "error"});
                        }
                    }
                });
        	});
        });
    });

    //格式化时间
    function renderDate(rowdata, index, value) {
        var dateStr = "";
        if (null != value) {
            var date = new Date(value);
            dateStr = DateUtils.formatDate(date, DateUtils.YYYY_MM_DDHHMMSS);
        }
        return dateStr;
    }
    
</script>

</body>
</html>
